﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using System.Xml;
using Microsoft.Practices.Unity;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Presentation;

namespace SpLocalizer.ContentSourceProviders.SpDocumentLibraryFile
{

    internal class PowerPointFileLocalizationTask : AbstractFileLocalizationTask
    {

         /// <summary>
        /// Constructor without parameters should be public for unity injection
        /// </summary>
        public PowerPointFileLocalizationTask () { }

        private const string SlideXmlElementName = "Slide";

        protected override string GetDocumentXmlToTranslate(SPListItem item)
        {            
            // Open a PresentationDocument for read-only access based on a stream.
            using (PresentationDocument presentationDocument = PresentationDocument.Open(item.File.OpenBinaryStream(), false))
            {
                
                XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.LoadXml(@"<PresentationDocument></PresentationDocument>");

                foreach(SlidePart slide in presentationDocument.PresentationPart.SlideParts){
                    XmlElement SlideNode = XmlDoc.CreateElement(SlideXmlElementName);
                    SlideNode.InnerXml = slide.Slide.InnerXml;                    
                    XmlDoc.DocumentElement.AppendChild(SlideNode);
                }

                if (XmlDoc.DocumentElement.ChildNodes.Count == 0)
                    return null;
                
                using (StringWriter sw = new StringWriter()){
                using(XmlTextWriter xtw = new XmlTextWriter(sw)){    
                    XmlDoc.WriteContentTo(xtw);
                    xtw.Flush();
                    sw.Flush();
                    return sw.ToString();
                }
                }
            }
            
        }

        internal override void ExportTranslation(SPListItem item)
        {

            if (!Constants.FILE_POWERPOINT.Equals(this.Format))
            {
                throw new ApplicationException(String.Format("Illegal format {0} for export with {1}. {2} Required",
                    this.Format, typeof(PowerPointFileLocalizationTask).Name, Constants.FILE_POWERPOINT));
            }
                SPFile theFile = item.File;


                // Open a WordprocessingDocument for read-only access based on a stream.
                Stream documentStream = theFile.OpenBinaryStream();
                using (PresentationDocument pptxDocument = PresentationDocument.Open(documentStream, true))
                {

                    XmlDocument XmlDoc = new XmlDocument();
                    XmlDoc.LoadXml(this.TranslatedText);

                    int SlideNumber = 0;
                    foreach (SlidePart slide in pptxDocument.PresentationPart.SlideParts)
                    {

                        slide.Slide.InnerXml = XmlDoc.DocumentElement.ChildNodes[SlideNumber++].InnerXml;
                        
                    }

                    pptxDocument.Close();

                    SafeFile(theFile, documentStream);

                }
            

        }
    }
}
